package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wechatdaka.entity.Menu;
import com.example.wechatdaka.entity.MenuNode;
import com.example.wechatdaka.service.MenuService;
import com.example.wechatdaka.utils.MenuIdUtil;
import com.example.wechatdaka.utils.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-05
 */
@Api(tags = "菜单模块")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;



    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAllMenu(){

        QueryWrapper<Menu> menuQueryWrapper=new QueryWrapper<>();
        menuQueryWrapper.isNull("lxFather").orderByAsc("lxOrder");
        List<Menu> menuFatherList = menuService.list(menuQueryWrapper);
        for (int i=0;i<menuFatherList.size();i++){
            QueryWrapper<Menu> menuChildQueryWrapper=new QueryWrapper<>();
            menuChildQueryWrapper.eq("lxFather", menuFatherList.get(i).getId());
            List<Menu> menuChildrenList =menuService.list(menuChildQueryWrapper);
            menuFatherList.get(i).setChildMenu(menuChildrenList);
        }
        return new Result(menuFatherList);
    }
//    @RequestMapping(value = "/queryAllMenuByUser",method = RequestMethod.POST)
//    public Result queryAllMenuByUser(String lxUserId){
//        String menuIdsByUser = menuService.queryMenuIdsByUser(lxUserId);
//        String[] split = menuIdsByUser.split(",");
//        List<String> menuIds= Arrays.asList(split);
//        QueryWrapper<Menu> menuQueryWrapper=new QueryWrapper<>();
//        menuQueryWrapper.in("id",menuIds).isNull("lxFather").orderByAsc("lxOrder");
//        List<Menu> menuFatherList = menuService.list(menuQueryWrapper);
//        for (int i=0;i<menuFatherList.size();i++){
//            QueryWrapper<Menu> menuChildQueryWrapper=new QueryWrapper<>();
//            menuChildQueryWrapper.in("id",menuIds).eq("lxFather", menuFatherList.get(i).getId()).orderByAsc("lxOrder");
//            List<Menu> menuChildrenList =menuService.list(menuChildQueryWrapper);
//            menuFatherList.get(i).setChildMenu(menuChildrenList);
//        }
//        return new Result(menuFatherList);
//    }
    @RequestMapping(value = "/queryMenuByUser",method = RequestMethod.POST)
    public Result queryMenuByUser(@RequestBody String lxUserId){
        String menuIdsByUser = menuService.queryMenuIdsByUser(lxUserId);
        String[] split = menuIdsByUser.split(",");
        List<String> menuIds= Arrays.asList(split);
        QueryWrapper<Menu> menuQueryWrapper=new QueryWrapper<>();
        menuQueryWrapper.in("id",menuIds).isNotNull("lxFather").orderByAsc("lxFather+0").orderByAsc("lxOrder+0");
        List<Menu> menuSubList = menuService.list(menuQueryWrapper);
        List<Menu> menuFatherList=new ArrayList<>();
        if (menuIds.contains("100")){
            Menu menu = menuService.getById("100");
            menuFatherList.add(menu);
        }
        for (int i=0;i<menuSubList.size();i++){
            QueryWrapper<Menu> menuFatherQueryWrapper=new QueryWrapper<>();
            String id=menuSubList.get(i).getLxFather();
            Menu fatherMenu = menuService.getById(id);
            if (!menuFatherList.contains(fatherMenu)){
                menuFatherList.add(fatherMenu);
            }
        }
        for (int i = 0; i < menuFatherList.size(); i++) {
            String fatherId = menuFatherList.get(i).getId();
            List<Menu> childMenu = new ArrayList<>();
            for (int j = 0; j < menuSubList.size(); j++) {
                String lxFather = menuSubList.get(j).getLxFather();
                if (fatherId.equals(lxFather)){
                    childMenu.add(menuSubList.get(j));
                }
            }
            menuFatherList.get(i).setChildMenu(childMenu);
        }
        return new Result(menuFatherList);
    }
    @RequestMapping(value = "/node",method = RequestMethod.POST)
    public Result queryAllMenuNode(){

        List<MenuNode> menuNodeFatherList=new ArrayList<>();
//        List<MenuNode> menuNodeChildrenList=new ArrayList<>();
        QueryWrapper<Menu> menuQueryWrapper=new QueryWrapper<>();
        menuQueryWrapper.orderByAsc("lxOrder");
        List<Menu> menuList = menuService.list(menuQueryWrapper);
        for (int i = 0; i < menuList.size(); i++) {
            Menu menu = menuList.get(i);
            if (menu.getLxFather()==null){  //父级为空，一级目录
                MenuNode menuNode = new MenuNode();
                menuNode.setLabel(menu.getLxMenuName());
                menuNode.setId(menu.getId());
                for (int j = 0; j < menuList.size(); j++) {
                    String lxFather = menuList.get(j).getLxFather();
                    if (menuNode.getId().equals(lxFather)){    //判断二级目录
                        MenuNode menuNodeChildren = new MenuNode();
                        menuNodeChildren.setLabel(menuList.get(j).getLxMenuName());
                        menuNodeChildren.setId(menuList.get(j).getId());
                        menuNode.getChildren().add(menuNodeChildren);
                    }
                }
                menuNodeFatherList.add(menuNode);
            }
        }

        return new Result(menuNodeFatherList);
    }
    @RequestMapping(value = "/menuIds",method = RequestMethod.POST)
    public Result querymenuIds(@RequestBody String [] arr){

        String str=String.join(",", arr);
        System.out.println("str"+str);



        return new  Result();
    }

}

